﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using sgsBase;

namespace gaClear
{
	class TcsDataToCPC
	{
		string _errMsg = "";
		long _clearId;
		long _clearId2;
		int _refId;
		string _cfi2 = "";
		string _tmUnit = "";
		double _tradePrice = 0;
		DbClearingTrade _clearTrade = new DbClearingTrade(); 
		DbVwTradeSummary _trade = new DbVwTradeSummary();

		public string ErrMsg
		{
			get { return _errMsg;}
			set { _errMsg = value;}
		}

		public TcsDataToCPC( long clearId, int refId)
		{
			_clearId = clearId;
			_refId = refId;
		}

		public bool ConvertData()
		{
			//' Do the following:
			//' - first, get the new trade type
			//' - gather existing TCS loan data
			//' - convert data over and save to database
			//' - determine if there should be more than 1 NYMEX trade
			//'   split it out and create a 'related' id on the second one
			//' - start verifying data
		
			pullCPCType();
			if (_clearTrade.ProductId == "")
			{
				_errMsg = "No NYMEX Product found for this Trade.";
				setStatus(ClearingStatuses.NoCPCID, _errMsg);
				return false;
			}

			DbClearingUnresolved cu = new DbClearingUnresolved();
			cu.ClearId = CASTHelper.Integer(_clearId);
			cu.Delete();

			fillCPCData();

			if (_errMsg != "")
			{
				setStatus(ClearingStatuses.SubmissionError, _errMsg);
				return false;
			}

			moveDataToTable();
			runDataChecks();

			if (_errMsg != "")
			{
				//dont log error message because it is logged in unresolved module
				return false;
			}

			SubmitForClearing();
			return true;
		}

		private void pullCPCType()
		{
			DbVwClearingTcsCpcXr vctc = new DbVwClearingTcsCpcXr();
			vctc.RefId = _refId;
			if (!vctc.Select())
				return;

			_clearTrade.ProductId = vctc.CpcProdId;
			_clearTrade.FirstCfi = vctc.Cfi;
			_clearTrade.LastCfi = vctc.Cfi;
			_clearTrade.TradePrice = vctc.Price;
			_cfi2 = vctc.Cfi2;
	    }

		/*
			Me.m_tradeprice = dbi.getval("TrPricePremium")
			If Me.m_tradeprice = "0" Then
				Me.m_tradeprice = dbi.getval("TrStrikePrice")
			End If
		 */
		public double TradePrice
		{
			get	{	return _tradePrice;	}
			set { _tradePrice = value; }
		}
		public string BuyCustId
		{
			get
			{
				if (_trade.BCoClearId == "")
					return _trade.TrBCoName;
				return _trade.BCoClearId;
			}
		}
		public string BuyTraderId
		{
			get
			{
				return _trade.TrBTrader;
			}
		}
		public string SellTraderId
		{
			get
			{
				return _trade.TrSTrader;
			}
		}
		public string SellCustId
		{
			get
			{
				if (_trade.SCoClearId == "")
					return _trade.TrSCoName;
				return _trade.SCoClearId;
			}
		}
		public double FirstStrikePrice
		{
			get
			{
				return _trade.TrStrikePrice;
			}
		}
		public double LastStrikePrice
		{
			get
			{
				return _trade.TrStrikePrice;
			}
		}
		public double TradeQty
		{
			get
			{
				return _trade.MonthlyVol;
			}
		}
		public DateTime FirstMMY
		{
			get
			{
				return _trade.TrPStart;
			}
		}
		public DateTime LastMMY
		{
			get
			{
				return _trade.TrPEnd;
			}
		}
		public int BrokerId
		{
			get { return _trade.TrSBrokerId; }
		}

		private void fillCPCData()
		{
			_trade.Reset();
			_trade.RefId = CASTHelper.Integer(_refId);
			if (_trade.Select())
			{
				_errMsg = "Error, Trade not found";
				return;
			}

			/*
				Me.m_tradeqty = dbi.getval("MonthlyVol")
				Me.m_firstmmy = dbi.getval("TrPStart")
				Me.m_lastmmy = dbi.getval("TrPEnd")
				Me.m_firststrike = dbi.getval("TrStrikePrice")
				Me.m_buy_custid = dbi.getval("BCoClearId")
				Me.m_buy_traderid = dbi.getval("TrBTrader")
				Me.m_sell_custid = dbi.getval("SCoClearId")
				Me.m_sell_traderid = dbi.getval("TrSTrader")
				Me.m_brokerid = dbi.getval("TrSBrokerId")
				Me.m_laststrike = Me.m_firststrike
			 */
			if (_trade.TrPricePremium == 0)
				TradePrice = _trade.TrStrikePrice;
			else
				TradePrice = _trade.TrPricePremium;

			DbTradeVolFreqOpt tvfo = new DbTradeVolFreqOpt();
			tvfo.TradeVolFreqId = _trade.TradeVolFreqId;
			if (tvfo.Select())
				_tmUnit = tvfo.TmUnit;
		}

		private void moveDataToTable()
		{
			string clause = "";
			// before sending data to table, determine if it is a 1 month or multi-month trade
			if (FirstMMY.Month == LastMMY.Month && FirstMMY.Year == LastMMY.Year)
				_trade.TrPEnd = DateTime.MinValue;

			//'if this trade should be split into two nymex trades, then do that here
			if (_cfi2 != "")
			{
				if (_clearTrade.TradePrice != "")
				{
					if (_clearTrade.TradePrice == "TrPricePremium/2")
						TradePrice = TradePrice / 2;
				}

				DbClearingTrade ct = new DbClearingTrade();
				ct.RelatedClearId = CASTHelper.Integer(_clearId);
				ct.RefId = _refId;
				ct.DtQueued = DateTime.Now;
				ct.ClearingHouse = 43;
				ct.BrokerDisplay = 1;
				ct.FirstCfi = _cfi2;
				if (LastMMY != DateTime.MinValue)
					ct.LastCfi = _cfi2;
				ct.Insert();

				_clearId2 = ct.ClearId;

				clause = "clear_id=" + _clearId2;
			}

			_clearTrade.ClearingStatus = ClearingStatuses.GatheringData.GetHashCode();
			_clearTrade.ClearingBrokerId = BrokerId;
			_clearTrade.TradePrice = TradePrice.ToString();
			_clearTrade.TradeQty = TradeQty.ToString();
			_clearTrade.FirstMmy = FirstMMY.ToString();
			_clearTrade.TmUnit = _tmUnit;
			_clearTrade.FirstStrike = FirstStrikePrice.ToString();
			if (LastMMY != DateTime.MinValue)
			{
				_clearTrade.LastMmy = LastMMY.ToString();
				_clearTrade.LastStrike = LastStrikePrice.ToString();
			}
			_clearTrade.BuyCustomerId = BuyCustId;
			_clearTrade.BuyTraderId = BuyTraderId;
			_clearTrade.SellCustomerId = SellCustId;
			_clearTrade.SellTraderId = SellTraderId;
			if (_clearId2 != 0)
				_clearTrade.RelatedClearId = CASTHelper.Integer(_clearId2);

			if (clause != "") _clearTrade.OrClause = clause;

			_clearTrade.Update();
		}

		private void setStatus(ClearingStatuses newstat, string msgtxt = "")
		{
			//_clearTrade.ClearValues();
			_clearTrade.ClearingStatus = newstat.GetHashCode();
			_clearTrade.MessageText = msgtxt;
			_clearTrade.Update();

			//make sre to also take care of clearTrade2
		}

	}
}
